//
// Created by bjliuyong on 2021/6/7.
//

#include "memory_cell.h"
#include "memory_chunk.h"

MemoryCell::~MemoryCell() {
    INFO_PRINT("[调用析构函数%s]释放资源 Cell start:%d Cell size:%d\n", __func__, m_start, m_size);
}

MemoryCell::MemoryCell(uint start, uint size) : m_start(start), m_size(size) {
    this->m_end = start + size;
    this->m_mark = false;
}

MemoryCell::MemoryCell(MemoryCell &cell) {
    this->m_start = cell.m_start;
    this->m_size = cell.m_size;
    this->m_end = cell.m_start + m_size;
    this->m_mark = false;
}

bool MemoryCell::operator>(MemoryCell &cell) {
    return m_start > cell.get_start();
}

bool MemoryCell::operator<(MemoryCell &cell) {
    return m_start < cell.get_start();
}

void MemoryCell::set_chunk(pvoid m_chunk) {
    this->m_chunk = m_chunk;
}

uint MemoryCell::get_start() {
    return this->m_start;
}

MemoryCell *MemoryCell::set_start(uint start) {
    this->m_start = start;
    return this;
}

MemoryCell *MemoryCell::inc_start(uint step) {
    this->m_start += step;
    return this;
}

uint MemoryCell::get_size() {
    return this->m_size;
}

MemoryCell *MemoryCell::desc_size(uint step) {
    this->m_size -= step;
    return this;
}

void MemoryCell::inc_size(uint step) {
    this->m_size += step;
}

void MemoryCell::set_size(uint size) {
    this->m_size = size;
}

uint MemoryCell::get_end() {
    return this->m_end;
}

void MemoryCell::set_end(uint end) {
    this->m_end = end;
}

void MemoryCell::set_mark(bool mark) {
    this->m_mark = mark;
}

bool MemoryCell::get_mark() {
    return this->m_mark;
}

pvoid MemoryCell::ptr() {
    if (NULL == m_chunk) {
        return NULL;
    }

    MemoryChunk *chunk = (MemoryChunk *) m_chunk;
    return (pvoid) ((ulong) chunk->get_addr() + get_start() * chunk->get_align_size());
}
